$(function() {
	$.common.initFormInfo();
	var url = ctx + "admin/role/roleDeptTreeData?roleId=" + $("#roleId").val();
	var options = {
		id: "deptTrees",
        url: url,
        check: { enable: true, nocheckInherit: true, chkboxType: { "Y": "ps", "N": "ps" } },
        expandLevel: 2
    };
	$.tree.init(options);
});

$("#editForm").validate({
	onkeyup: false,
	rules:{
	    roleName: {
	        required: true
	    },
	    roleCode: {
	        required: true
	    },
	    dsType: {
	        required: true
	    }
	},
	messages: {
		roleName: {
	        required: "请输入角色名称"
	    },
	    roleCode: {
	        required: "请输入角色编码"
	    },
	    dsType: {
	        required: "请选择数据范围"
	    }
	},
    focusCleanup: true
});

$("#dsType").change(function(event){
	var dataScope = $(event.target).val();
	dataScopeVisible(dataScope);
});

function dataScopeVisible(dataScope) {
	if (dataScope == 1) {
		$("#authDataScope").show();
	} else {
		$._tree.checkAllNodes(false);
		$("#authDataScope").hide();
	}
}

function submitHandler() {
    if ($.validate.form()) {
    	var deptIds = $.tree.getCheckedAllCheckNodes();
    	var dsType = $("#dsType").val();
    	if(dsType == '1' && !deptIds){
    		$.modal.alertError("请勾选自定义数据权限范围！");
    		return;
    	}
    	$('#dsScope').val(deptIds);
    	$.common.ajaxCommitForm($('#editForm'),false);
    	return false;
    }
}